<template>
  <div></div>
  <div>
    <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
      <template #toolbar>
        <div>
          状态：{{ statusName }}
          <a-button
            class="ml-1"
            type="link"
            @click="handleDispatch('stop')"
            v-if="statusName === '运行中'"
          >
            暂停调度
          </a-button>
          <a-button class="ml-1" type="link" @click="handleDispatch('start')" v-else>
            恢复调度
          </a-button>
        </div>
        <a-button type="primary" @click="handleCreate(DataType.FLOW)"> 新增流程调度 </a-button>
        <a-button type="primary" @click="handleCreate(DataType.SCRIPT)"> 新增脚本调度 </a-button>
        <a-button type="primary" @click="handleCreate(DataType.SUPSCRIBE)"> 新增订阅调度 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              title: '编辑',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                placement: 'leftBottom',
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DispatchCenterDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import {
    getAdminListJob,
    getAdminResumeJob,
    getAdminStopJob,
    getAdminDelJob,
  } from '/@/api/system/system';
  import { useDrawer } from '/@/components/Drawer';
  import DispatchCenterDrawer from './dispatchCenterDrawer.vue';
  import { columns, searchFormSchema, DataType } from './dispatchCenter.data';
  import { useMessage } from '/@/hooks/web/useMessage';
  export default defineComponent({
    name: 'DispatchCenter',
    components: { BasicTable, TableAction, DispatchCenterDrawer },
    setup() {
      const { createMessage, createConfirm } = useMessage();
      const [registerDrawer, { openDrawer }] = useDrawer();
      onMounted(async () => {
        getStatusName();
      });

      const [registerTable, { reload, getSelectRowKeys }] = useTable({
        title: '调度中心',
        api: getAdminListJob,
        columns,
        formConfig: {
          labelWidth: 80,
          schemas: searchFormSchema,
        },
        searchInfo: {
          op: 'search',
        }, //额外的参数
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate(type) {
        openDrawer(true, {
          isUpdate: false,
          type,
        });
      }

      function handleEdit(record: Recordable) {
        if (!record.jobClass) {
          createMessage.warning('类名称为空不能编辑');
          return;
        }
        const type = record.jobClass;
        openDrawer(true, {
          record,
          isUpdate: true,
          type,
        });
      }

      async function handleDelete(record: Recordable) {
        await getAdminDelJob({ id: record.id }).then(() => {
          handleSuccess();
          createMessage.success('操作成功');
        });
      }

      //暂定调度
      const handleDispatch = async (type) => {
        if (type == 'stop') {
          await getAdminStopJob();
        } else if (type == 'start') {
          await getAdminResumeJob();
        }
        createMessage.success('操作成功');
        getStatusName();
      };
      const statusName = ref('');
      //获取状态
      const getStatusName = async () => {
        const res = await getAdminListJob({ pageSize: 1 });
        statusName.value = res.statusName;
      };

      function handleSuccess() {
        reload();
      }
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleDispatch,
        DataType,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
        statusName,
      };
    },
  });
</script>
